/**
 * Private Variable Declaration
 * */
 import Modules.Cities.CityDetail;
 
 import com.desidea.common.CommonClass;
 import com.desidea.components.grid.SuperGridEvent;
 import com.desidea.components.grid.button.SuperGridButton;
 import com.desidea.components.spinner.BusySpinner;
 import com.desidea.webservice.PartnerService;
 import com.desidea.webservice.ProcessServiceRequestResultEvent;
 
 import flash.events.Event;
 import flash.events.MouseEvent;
 
 import mx.controls.Alert;
 import mx.controls.Button;
 import mx.controls.CheckBox;
 import mx.core.Application;
 import mx.managers.PopUpManager;
 

private var chkAllCities:CheckBox; 
/**
 * Event Handling - CreationComplete event
 * */
private function windowCreated():void{
	BusySpinner.getInstance().play(Application.application.ModuleCvs);

	chkAllCities = new CheckBox();
	chkAllCities.selected = false;
	chkAllCities.label = "Show All Cities";
	chkAllCities.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void{
														loadData();
													});

	var newItemBtn:Button = new Button();
	newItemBtn.label = "Create New City";
	newItemBtn.addEventListener(MouseEvent.CLICK, function(event:Event):void{
														showDetail(SuperGridButton.MODE_EDIT,"");
													});

//	citiesGrid.toolBar.addChild(chkAllCities);
	citiesGrid.toolBar.addChild(newItemBtn);
	
	this.citiesGrid.addEventListener(SuperGridEvent.CLICKED, gridButtonColumnClicked);
	loadData();
}


public function loadData():void{
	
	var params:String = "OPERATION=CITIES_LIST" +
//						(chkAllCities.selected?"":"&ISACTIVE=X") +
						"&USERID="+CommonClass.UserID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForCityList);
	ws.processServiceRequest(params);
}

private function callForCityList(event:ProcessServiceRequestResultEvent):void{
	
	var result:XML = XML(event.result);
	if(result.RESPONSE.RESULT == 'S'){
		this.citiesGrid.dataProvider = result;
		setColumnWidths();
	}else{
		Alert.show( result.RESPONSE.MESSAGE );
	}
	BusySpinner.getInstance().stop();
}

private function setColumnWidths():void{
	CommonClass.setColumnWidth(this.citiesGrid,1, 80,"right");
	CommonClass.setColumnWidth(this.citiesGrid,2, 200);
	CommonClass.setColumnWidth(this.citiesGrid,3, 90,"center");
}

private function gridButtonColumnClicked(event:SuperGridEvent):void{
	handleGridButtonClick(event);
}

private function handleGridButtonClick(event:SuperGridEvent, moduleCode:String = null):void{
	var mode:String =  String(event.data.linkId).toLowerCase();
	
	if(mode == SuperGridButton.MODE_EDIT || mode == SuperGridButton.MODE_VIEW || mode == SuperGridButton.MODE_VIEWICON){
		showDetail(mode, event.data.data["key"].toString());
	}else{
		Alert.show("Mode : " + mode + " not handled");
	}
}

private function showDetail(mode:String, key:String=""):void{
	var cd:CityDetail = new CityDetail();
	cd.key = key;
	cd.caller = this;
	
	PopUpManager.addPopUp(cd,this,true);
	PopUpManager.centerPopUp(cd);
}